/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yy.bugzillahelp.action.product;

import com.yy.bugzillahelp.data.ComponentData;
import com.yy.bugzillahelp.tool.SQLTool;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hualun-alan
 */
public class ComponentAction {

    private static final Logger logger = Logger.getLogger(VersionAction.class.getName());

    public static int insertOne(Connection con, ComponentData componentData, String serverId) {
        String sql1 = "insert into components(component_id,name,desc,default_assigned_to,product_id,server_id)"
                + " values (?,?,?,?,?,?)";

        PreparedStatement ps1 = null;
        try {
            con.setAutoCommit(false);

            ps1 = con.prepareStatement(sql1);
            ps1.setString(1, componentData.getId());
            ps1.setString(2, componentData.getName());
            ps1.setString(3, componentData.getDesc());
            ps1.setString(4, componentData.getDefaultAssignTo());
            ps1.setString(5, componentData.getProductId());
            ps1.setString(6, serverId);
            ps1.executeUpdate();

            con.commit();
            return 1;
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, "sql1 = {0}", sql1);
            logger.log(Level.SEVERE, null, ex);
            return -1;
        } finally {
            SQLTool.closeStatement(ps1);
        }
    }

    public static int insertList(Connection con, ArrayList<ComponentData> componentDatas, String serverId) {
        String sql1 = "insert into components(component_id,name,desc,default_assigned_to,product_id,server_id)"
                + " values (?,?,?,?,?,?)";

        PreparedStatement ps1 = null;
        try {
            con.setAutoCommit(false);

            ps1 = con.prepareStatement(sql1);
            ps1.setString(6, serverId);
            for (ComponentData componentData : componentDatas) {
                ps1.setString(1, componentData.getId());
                ps1.setString(2, componentData.getName());
                ps1.setString(3, componentData.getDesc());
                ps1.setString(4, componentData.getDefaultAssignTo());
                ps1.setString(5, componentData.getProductId());
                ps1.executeUpdate();
            }

            con.commit();
            return 1;
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, "sql1 = {0}", sql1);
            logger.log(Level.SEVERE, null, ex);
            return -1;
        } finally {
            SQLTool.closeStatement(ps1);
        }
    }

    public static ArrayList<ComponentData> searchComponents(Connection con, String productId, String serverId) {
        ArrayList<ComponentData> componentDatas = new ArrayList<ComponentData>();

        String sql1 = "select * from components where product_id = ? and server_id = ? order by component_id";
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql1);
            ps.setString(1, productId);
            ps.setString(2, serverId);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                ComponentData componentData = new ComponentData();
                componentData.setId(rs.getString("component_id"));
                componentData.setName(rs.getString("name"));
                componentData.setDesc(rs.getString("desc"));
                componentData.setDefaultAssignTo(rs.getString("default_assigned_to"));
                componentData.setProductId(rs.getString("product_id"));
                componentDatas.add(componentData);
            }
        } catch (SQLException ex) {
            logger.log(Level.SEVERE, null, ex);
        } finally {
            SQLTool.closeStatement(ps);
        }

        return componentDatas;
    }

    public static int delete(Connection con, String productId, String serverId) {
        return SQLTool.executeUpdate(con, "delete from components where product_id = '" + productId + "' and server_id = '" + serverId + "'");
    }
}
